import request from '@/api/axiosRequest'
import {
  ListContainersParamsFilterTy,
  ListNetWorkFiltersTy,
} from './../types/docker.type'

/**
 * 查看docker 信息
 */
export const viewDockerReq = () => {
  return request({
    url: '/info',
    method: 'get',
  })
}

/**创建容器 */
export const createOneContainerReq = (name: string, data: any) => {
  
  return request({
    url: '/containers/create',
    method: 'post',
    params: { name },
    data: {
      Tty: true,
      ...data,
    },
  })
}

/**查询容器详细信息 */
export const inspectContainerReq = (id: string) => {
  return request({
    url: `/containers/${id}/json`,
  })
}

/**列出容器 */
export const listContainerReq = async (filters?: ListContainersParamsFilterTy) => {

    return request({
    url: '/containers/json',
    params: {
      all: true,
      filters: JSON.stringify(filters),
    },
  })
}

/**强制删除容器 */
export const deleteContainer = (id: string) => {
  return request({
    method: 'delete',
    url: `/containers/${id}`,
    params: { force: true },
  })
}

/**改变容器状态 */
export const changeContainerStatusReq = (params: {
  id: string
  sign: 'start' | 'restart' | 'stop' | 'kill'
}) => {
  return request({
    method: 'post',
    url: `/containers/${params.id}/${params.sign}`,
  })
}

/**列出所有网络 */
export const listNetworkReq = (filters: ListNetWorkFiltersTy = {}) => {
  return request({
    url: '/networks',
    params: {
      filters: JSON.stringify({
        ...filters,
      }),
    },
  })
}

/**创建网络 */
export const createNetworkReq = (name: string) => {
  return request({
    url: '/networks/create',
    method: 'post',
    data: { name },
  })
}

/**删除网络 */
export const removeNetworkReq = (idOrName: string) => {
  return request({
    url: `/networks/${idOrName}`,
    method: 'delete',
  })
}

/**创建命令 */
export const createExec = (container_id: string, script_name: string) => {
  return request({
    url: `/containers/${container_id}/exec`,
    method: 'post',
    data: {
      AttachStdout: true,
      AttachStdin: true,
      Tty: false,
      Cmd: [
        'bash',
        '-c',
        `bash /opt/myshell/${script_name}.sh username2 password2 windows table`,
      ], //TODO 后面这里需要改成动态的
      User: 'root',
    },
  })
}

/**执行命令 */
export const startExec = (exec_id: string) => {
  return request({
    url: `/exec/${exec_id}/start`,
    method: 'post',
    data: {},
  })
}

